package cascade.components.skins
{

import flash.filters.BlurFilter;
import flash.geom.Matrix;

import mx.skins.ProgrammaticSkin;

public class TabSkin extends ProgrammaticSkin
{
	
	
	protected override function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
	{
		super.updateDisplayList(unscaledWidth, unscaledHeight);
		
		var x:Number = 0;
		var y:Number = 0;
		var width:Number = unscaledWidth;
		var height:Number = unscaledHeight;
		var corner:Number = getStyle("cornerRadius")*2;
		var borderColor:int = getStyle("borderColor");
		
		graphics.clear();
		graphics.beginFill(0, 0);
		drawTab(x, y, width, height, corner);
		
		var m:Matrix;
		
		var isSelected:Boolean = name.indexOf("selected") == 0;
		
		if (isSelected)
			height++;
		
		if (isSelected) {
			// border
			graphics.beginFill(borderColor, 1);
			drawTab(x, y, width, height, corner, isSelected);
			drawTab(x + 1, y + 1, width - 2, height - 1, corner - 2, isSelected);
			
			// background gradient
			m = verticalGradientMatrix(x + 1, y + 1, width - 2, height - 2);
			graphics.beginGradientFill("linear", [0xE7F2FF, 0xB0CBEF], [1, 1], [0, 255], m);
			drawTab(x + 1, y + 1, width - 2, height - 1, corner - 2, isSelected);
			
			// highlight border
			graphics.beginFill(0xFFFFFF, .4);
			drawTab(x + 1, y + 1, width - 2, height - 3, corner - 2, isSelected);
			drawTab(x + 2, y + 2, width - 4, height - 4, corner - 4, isSelected);
			
			// top highlight
			m = verticalGradientMatrix(x + 1, y + 1, width - 2, 4);
			graphics.beginGradientFill("linear", [0xFFFFFF, 0xFFFFFF], [.6, .2], [0, 255], m);
			drawTab(x + 1, y + 1, width - 2, 4, corner - 2);
			
			if (name == "selectedOverSkin") {
				filters = [new BlurFilter(1, 1)];
				graphics.beginFill(0xFFCC00, .6);
				drawTab(x - 1, y - 1, width + 2, height, corner + 2, isSelected);
				drawTab(x + 1, y + 1, width - 2, height - 2, corner - 2, isSelected);
			}
		} else if (name == "overSkin") {	
			// border
			graphics.beginFill(borderColor, 1);
			drawTab(x, y, width, height, corner, isSelected);
			drawTab(x + 1, y + 1, width - 2, height - 1, corner - 2, isSelected);
			
			m = new Matrix();
			m.createGradientBox(width*2, height*2, 0, x - width/2, y - height);
			graphics.beginGradientFill("radial", [0xFF9900, 0xFF9900], [0, .3], [30, 255], m);
			drawTab(x + 1, y + 1, width - 2, height - 1, corner - 2);
			
			graphics.beginFill(0xFFFFFF, .4);
			drawTab(x + 1, y + 1, width - 2, height - 1, corner - 2);
			drawTab(x + 2, y + 2, width - 4, height - 2, corner - 4);
		}
	}
	
	public function drawTab(x:Number, y:Number, width:Number, height:Number, corner:Number, selected:Boolean = false):void {
		corner /= 2;
		if (selected) {
			graphics.moveTo(x - corner, y + height);
			graphics.curveTo(x, y + height, x, y + height - corner);
		} else {
			graphics.moveTo(x, y + height);
		}
		graphics.lineTo(x, y + corner);
		graphics.curveTo(x, y, x + corner, y);
		graphics.lineTo(x + width - corner, y);
		graphics.curveTo(x + width, y, x + width, y + corner);
		if (selected) {
			graphics.lineTo(x + width, y + height - corner);
			graphics.curveTo(x + width, y + height, x + width + corner, y + height);
		} else {
			graphics.lineTo(x + width, y + height);
			graphics.lineTo(x, y + height);
		}
	}
}
}